﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.Reporting.WebForms;

namespace VSM
{
    public partial class FormPrinting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                hdnDocEntry.Value = Request.QueryString["DocEntry"];
                hdnDocType.Value = Request.QueryString["DocType"];
                hdnPrintType.Value = Request.QueryString["PrintType"];

                string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
                // Load & binding data
                LoadFormPrinting(dbType);
            }
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            //ReportDocument rptDoc = new ReportDocument();
            //rptDoc.Load(Server.MapPath(rptTemplatePath));
            //rptDoc.PrintToPrinter(1, false, 0, 0);
        }

        private void LoadFormPrinting(string dbType)
        {
            GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
            DataTable dtReport = null, dtSubReport = null, dtSubReport1 = null, dtSubReport2 = null, dtSubReport3 = null;
            string rptTemplatePath = string.Empty, lsReportName, lsSubReportName, lsSubReportName1, lsSubReportName2, lsSubReportName3;
            lsReportName = lsSubReportName = lsSubReportName1 = lsSubReportName2 = lsSubReportName3 = string.Empty;

            try
            {
                switch (hdnDocType.Value)
                {
                    case "18": // AP INVOICE
                        break;

                    case "19": // AP CREDIT
                        break;

                    case "20": // GOODS RECEIPT PO
                        break;

                    case "21": // GOODS RETURN
                        break;

                    case "22": // PURCHASE ORDER
                        if(dbType.Equals(Utils.AppConstants.VSM))
                        {
                            rptTemplatePath = "Sales_PO_VSM_MainReport.rpt";
                            //dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_Dealer where DocEntry = " + hdnDocEntry.Value);
                            dtSubReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_TotalOrderQty where DocEntry = " + hdnDocEntry.Value);
                            dtSubReport1 = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_ColorByColor where DocEntry = " + hdnDocEntry.Value);
                            dtSubReport2 = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_ShippingSch where DocEntry = " + hdnDocEntry.Value);
                            dtSubReport3 = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_Forecasts where DocEntry = " + hdnDocEntry.Value);
                            lsSubReportName = "Sales_PO_VSM_TotalOrderQty.rpt";
                            lsSubReportName1 = "Sales_PO_VSM_ColorByColor.rpt";
                            lsSubReportName2 = "Sales_PO_VSM_Shipping Sch.rpt";
                            lsSubReportName3 = "Sales_PO_VSM_Forecast.rpt";
                        }
                        else
                        {
                            rptTemplatePath = "Sales_PO_Dealer.rpt";
                            dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_Dealer where DocEntry = " + hdnDocEntry.Value);
                            dtSubReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_Detail_Dealer where DocEntry = " + hdnDocEntry.Value);
                            lsSubReportName = "Sales_PO_Detail_Dealer.rpt";
                        }
                        break;
                    case "540000006": // PURCHASE QUOTATION"
                        break;

                    case "1470000113": // PURCHASE REQUEST"
                        break;

                    //-------------------SALES--------------------------
                    case "13": // AR INVOICE
                        break;

                    case "14": // AR CREDIT
                        break;

                    case "15": // DELIVERY
                        rptTemplatePath = "Sales_DeliveryOrder.rpt";
                        dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_DeliveryOrder_VSM where DocEntry = " + hdnDocEntry.Value);
                        break;

                    case "16": // RETURN
                        break;

                    case "17": // SALES ORDER
                        rptTemplatePath = "SalesConfirmedOrder.rpt";
                        dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_OrderConfirmed where DocEntry = " + hdnDocEntry.Value);
                        break;

                    case "23": // SALES QUOTATION
                        rptTemplatePath = "SalesQuotation.rpt";
                        dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_SalesQuotation where DocEntry = " + hdnDocEntry.Value);
                        dtSubReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_SalesFreight where DocEntry = " + hdnDocEntry.Value);
                        lsSubReportName = "SalesQuotationFreight.rpt";
                        break;

                    //---------------------INVENTORY----------------------
                    case "59": // GOODS RECEIPT
                        break;

                    case "60": // GOODS ISSUE
                        break;

                    case "67": // TRANSFER
                        if (hdnPrintType.Value.Equals("0"))
                        {
                            rptTemplatePath = "Sales_InventoryTransfer.rpt";
                            dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_InventoryTransfer where DocEntry = " + hdnDocEntry.Value);                        
                        }
                        else if (hdnPrintType.Value.Equals("1"))
                        {
                            rptTemplatePath = "Sales_InternalTransfer_Invoice.rpt";
                            dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_InternalTransfer_Invoice where DocEntry = " + hdnDocEntry.Value);
                        }
                        else if (hdnPrintType.Value.Equals("2"))
                        {
                            rptTemplatePath = "Sales_Consignment_StockoutSlip.rpt";
                            dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_Consignment_StockoutSlip where DocEntry = " + hdnDocEntry.Value);
                        }

                        break;

                    case "1250000001": // TRANSFER REQUEST
                        break;

                    //-------------------MASTER DATA------------------
                    case "171": // EMPLOYEE
                        break;
                    case "2": // BUSINESS PARTNER
                        break;

                    case "4": // ITEM
                        break;

                    case "11": // CONTACT PERSON
                        break;

                    case "12": // USER
                        break;

                    //----------------------OTHERS--------------------
                    case "221": // Attachment
                        break;

                    case "97": // Sales opportunity
                        break;

                    case "33": // Activity
                        break;

                    case "30": // Journal Entry
                        break;

                    // -------------------PRODUCTION--------------------
                    case "202": // Production Order
                        break;

                    case "198": // SALES FORECASTS
                        break;

                    //--------------------PAYMENT----------------------
                    case "24": // Incoming
                        break;

                    case "46": // Outgoing
                        break;
                }
                // Initial report template
                ReportDocument rptDoc = new ReportDocument();
                CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
                CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
                rptDoc.Load(Server.MapPath("/Business/Printing/" + rptTemplatePath));

                // Load data
                if (dtReport != null) rptDoc.SetDataSource(dtReport);

                if (dtSubReport != null) rptDoc.Subreports[lsSubReportName].SetDataSource(dtSubReport);
                if (dtSubReport1 != null) rptDoc.Subreports[lsSubReportName1].SetDataSource(dtSubReport1);
                if (dtSubReport2 != null) rptDoc.Subreports[lsSubReportName2].SetDataSource(dtSubReport2);
                if (dtSubReport3 != null) rptDoc.Subreports[lsSubReportName3].SetDataSource(dtSubReport3);
                // Binding
                CrystalReportViewer1.ReportSource = rptDoc;
                CrystalReportViewer1.RefreshReport();
                CrystalReportViewer1.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
            }
        }
    }
}